package modeller.databasedesignmodel.transaction;

import modeller.databasedesignmodel.servicedemand.transaction.proceduralstatement.IProceduralStatementServiceDemandCalculator;
import modeller.databasedesignmodel.servicedemand.IServiceDemand;

import java.io.Serializable;

/**
 * Created by:  Jason Ye
 * Date:        25/02/2012
 * Time:        12:52
 */
public class Branch implements IProceduralStatement,Serializable {

    private double trueBranchExecProbability;
    private IProceduralStatement trueStatement;
    private IProceduralStatement falseStatement;

    public Branch(double trueBranchExecProbability, IProceduralStatement trueStatement, IProceduralStatement falseStatement) {
        this.trueBranchExecProbability = trueBranchExecProbability;
        this.trueStatement = trueStatement;
        this.falseStatement = falseStatement;
    }

    public Branch(double trueBranchExecProbability) {
        this.trueBranchExecProbability = trueBranchExecProbability;

    }

    public double getTrueBranchExecProbability() {
        return trueBranchExecProbability;
    }

    public void setTrueBranchExecProbability(double trueBranchExecProbability) {
        this.trueBranchExecProbability = trueBranchExecProbability;
    }

    public IProceduralStatement getTrueStatement() {
        return trueStatement;
    }

    public void setTrueStatement(IProceduralStatement trueStatement) {
        this.trueStatement = trueStatement;
    }

    public IProceduralStatement getFalseStatement() {
        return falseStatement;
    }

    public void setFalseStatement(IProceduralStatement falseStatement) {
        this.falseStatement = falseStatement;
    }

    @Override
    public String myToString() {
        
        StringBuffer buf = new StringBuffer();
        
        buf.append("BRANCH statement with condition probability " + trueBranchExecProbability +":\n");
        buf.append("TRUE BRANCH:\n");
        buf.append(trueStatement.toString());
        buf.append("\nFALSE BRANCH:\n");
        buf.append(falseStatement.toString());
        buf.append("End of BRANCH statement");

        return buf.toString();
    }

    @Override
    public IServiceDemand acceptCalculation(IProceduralStatementServiceDemandCalculator visitor) {
        return visitor.calculate(this);
    }
}
